﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.FileIO;

namespace Buddy.Samples
{
    static partial class Program
    {
        static void Samples_CSV()
        {
            var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "sample.csv");

            CreateSampleCsv(path);

            var csv = new TextFieldParser(path, Encoding.UTF8);
            csv.SetDelimiters(",");

            ReCreateSampleCsv(csv);

            Console.WriteLine("press any key to continue ...");
            Console.ReadKey();
        }

        static void ReCreateSampleCsv(TextFieldParser csv)
        {
            using (var writer = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "sample-re-created.csv"), false, Encoding.UTF8))
            {
                foreach (var rec in csv.Records(false))
                {
                    foreach (var field in rec) Console.Write("\t" + field);
                    Console.WriteLine();

                    writer.WriteRecord(",", rec);
                }
            }
        }

        static void CreateSampleCsv(string path)
        {
            using (var writer = new StreamWriter(path, false, Encoding.UTF8))
            {
                for (int i = 0; i < 10; i++)
                {
                    writer.WriteRecord(",", new object[] { i, "REC,#" + i, DateTime.Now.AddMinutes(i), "look at \"this\"", "کد یگانه" });
                }
            }
        }
    }
}
